package com.de.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Date 2020/2/27
 * @Author
 * @Version
 **/
@Slf4j
@Controller
public class LoginController {


    @GetMapping("/tologin")
//    @ResponseBody
    public String login(HttpServletRequest request){
//        UsernamePasswordToken token = new UsernamePasswordToken(account,password);
//        Subject subject = SecurityUtils.getSubject();
//        subject.login(token);
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        // 根据shiro返回的异常类路径判断，抛出指定异常信息
        if (exceptionClassName != null) {
            if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
                // 最终会抛给异常处理器
                log.info("账号不存在");
            } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
                log.info("用户名/密码错误");
            } else if ("randomCodeError".equals(exceptionClassName)) {
                log.info("验证码错误 ");
            } else {
                log.info("不是账户错误，也不是密码错误");// 最终在异常处理器生成未知错误
            }
        }
//        System.out.println("登录成功");
        return "/login";
    }
}
